/*
 * Copyright 2015 William Wang.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package wxk.Bonus.database;

/**
 * Created on : Jan 20, 2015 8:19:18 PM
 * @author William Wang
 */

import java.io.BufferedReader;
import java.io.File;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
 import java.sql.Connection;  
 import java.sql.DriverManager;  
 import java.sql.ResultSet;  
 import java.sql.Statement;  
import java.util.List;
import wxk.Bonus.utils.BonusLogger;
import wxk.Bonus.utils.Toolkit;
public class DBFactory {
private final String DRIVER     ="org.sqlite.JDBC";
private final String DB_FILE    =System.getProperty("user.home")+"\\doubleball.db3";
private final String DB_CONN_STR="jdbc:sqlite:"+DB_FILE;
String SQLFile[]={"_DBHISTORY.sql","_FORECAST.sql","allindex.sql"};
String checktable="SELECT  count(*) FROM sqlite_master WHERE type='table' AND name='_DBHISTORY'";

 boolean needinititalize =false;
 
 public DBFactory()
 {
  BonusLogger.getInstance().log(DB_FILE);
 }
 
 public boolean checkTableExist()
 {
       boolean result=false;
   
  File file=new File(DB_FILE);   
  if(!file.exists() )
  {
   return false;
  }
  //db file exits,but check table exits
      result=false;
     try(
             Connection conn=getConnection();
             Statement stmt = conn.createStatement();
      )
     {
     try( ResultSet rset = stmt.executeQuery(checktable);)
     {
            if(rset.next())
            {
                int count =rset.getInt(1);
                if(count>0)
                {
                result=true;
                }
            }
     }
     }catch(Exception e)
     {
         e.printStackTrace();
     }
     return result;
 }
 public Connection getConnection()
 {
     Connection connection = null;  
     ResultSet resultSet = null;  
     Statement statement = null;  
    try
    {
         Class.forName(DRIVER);  
         connection = DriverManager.getConnection(DB_CONN_STR);  
         statement = connection.createStatement(); 
    }catch(Exception e)
    {
        e.printStackTrace();
    }
   return connection; 
 }
 public void initTables()
 {
    Toolkit tk=new Toolkit();
    String str="";
   try(   Connection conn=getConnection(); )
   {
   Statement stmt =  conn.createStatement();
   StringBuilder sb=new StringBuilder();
   
   for(int i=0;i<SQLFile.length;i++)
   {
    str =  tk.readFromJARFile("wxk/Bonus/database/sql/"+SQLFile[i]);
    BonusLogger.getInstance().log(str);
    stmt.executeUpdate(str);
    sb=new StringBuilder();
   }
   }catch(Exception e)
   {
       e.printStackTrace();
   }
 }
}
